package com.szgz.platform.model;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;

@Data
@TableName("user_file")
public class UserFile {
    
    @TableId(value = "file_id", type = IdType.ASSIGN_UUID)
    private String fileId;
    
    @TableField("user_id")
    private String userId;
    
    @TableField("file_name")
    private String fileName;
    
    @TableField("file_path")
    private String filePath;
    
    @TableField("file_size")
    private Long fileSize;
    
    @TableField("file_type")
    private String fileType;
    
    @TableField("file_hash")
    private String fileHash;
    
    @TableField("upload_time")
    private LocalDateTime uploadTime;
    
    @TableField("last_modified")
    private LocalDateTime lastModified;
    
    @TableField("is_directory")
    private Boolean isDirectory;
    
    @TableField("parent_path")
    private String parentPath;
    
    @TableField("permissions")
    private String permissions;
    
    @TableField("created_at")
    private LocalDateTime createdAt;
    
    @TableField("updated_at")
    private LocalDateTime updatedAt;
    
    @TableLogic
    @TableField("deleted")
    private Boolean deleted;
    
    // 构造函数
    public UserFile() {
        this.deleted = false;
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
        this.uploadTime = LocalDateTime.now();
        this.lastModified = LocalDateTime.now();
    }
    
    // 构造函数
    public UserFile(String userId, String fileName, String filePath, Long fileSize, String fileType) {
        this();
        this.userId = userId;
        this.fileName = fileName;
        this.filePath = filePath;
        this.fileSize = fileSize;
        this.fileType = fileType;
        this.isDirectory = false;
    }
    
    // 构造函数 - 目录
    public UserFile(String userId, String fileName, String filePath, Boolean isDirectory) {
        this();
        this.userId = userId;
        this.fileName = fileName;
        this.filePath = filePath;
        this.isDirectory = isDirectory;
        this.fileSize = 0L;
        this.fileType = "directory";
    }
    
    // 更新文件信息
    public void updateFileInfo(Long fileSize, String fileType) {
        this.fileSize = fileSize;
        this.fileType = fileType;
        this.lastModified = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }
    
    // 更新文件路径
    public void updateFilePath(String newPath) {
        this.filePath = newPath;
        this.updatedAt = LocalDateTime.now();
    }
    
    // 检查是否是目录
    public boolean isDirectory() {
        return Boolean.TRUE.equals(this.isDirectory);
    }
    
    // 检查是否是文件
    public boolean isFile() {
        return !isDirectory();
    }
    
    // 获取文件扩展名
    public String getFileExtension() {
        if (this.fileName == null || this.isDirectory()) {
            return "";
        }
        int lastDotIndex = this.fileName.lastIndexOf('.');
        return lastDotIndex > 0 ? this.fileName.substring(lastDotIndex + 1) : "";
    }
    
    // 获取文件大小（格式化）
    public String getFormattedFileSize() {
        if (this.fileSize == null || this.fileSize == 0) {
            return "0 B";
        }
        
        String[] units = {"B", "KB", "MB", "GB", "TB"};
        int unitIndex = 0;
        double size = this.fileSize;
        
        while (size >= 1024 && unitIndex < units.length - 1) {
            size /= 1024;
            unitIndex++;
        }
        
        return String.format("%.2f %s", size, units[unitIndex]);
    }
    
    // 检查文件是否可读
    public boolean isReadable() {
        return this.permissions != null && this.permissions.contains("r");
    }
    
    // 检查文件是否可写
    public boolean isWritable() {
        return this.permissions != null && this.permissions.contains("w");
    }
    
    // 检查文件是否可执行
    public boolean isExecutable() {
        return this.permissions != null && this.permissions.contains("x");
    }
    
    // 设置文件权限
    public void setPermissions(String permissions) {
        this.permissions = permissions;
        this.updatedAt = LocalDateTime.now();
    }
    
    // 获取父目录路径
    public String getParentDirectory() {
        if (this.filePath == null) {
            return "";
        }
        int lastSlashIndex = this.filePath.lastIndexOf('/');
        return lastSlashIndex > 0 ? this.filePath.substring(0, lastSlashIndex) : "/";
    }
} 